package edu.tms.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import edu.tms.bean.ApplicationInfo;
import edu.tms.crud.JdbcUtil;
import edu.tms.dao.common.CommnonDao;


/**
 * 入党申请dao
 * @author 任淋洋
 *
 */
public class ApplicationInfoDaoImp implements CommnonDao<ApplicationInfo>{
	/**
	 * 保存内容
	 * @param value
	 * @return 
	 * @throws Exception
	 */
	@Override
	public long save(ApplicationInfo value) throws Exception {
		ApplicationInfo app = (ApplicationInfo)value;
		String sql="insert into t_application_info values (null,?,?,?,?,?)";
		return JdbcUtil.insertAuto(sql,app.getStuId(),app.getApplicationContent(),app.getCreateTime(),app.getTrainState(),app.getApplicationState());
	}
	/**
	 * 修改内容
	 * @param value
	 * @throws Exception
	 */
	@Override
	public int update(ApplicationInfo value) throws Exception {
		ApplicationInfo app = (ApplicationInfo)value;
		String sql="update t_application_info set stu_id=?,application_content=?,create_time=?,train_state=?,application_state=? where application_info_id=?";
		return JdbcUtil.update(sql,app.getStuId(),app.getApplicationContent(),app.getCreateTime(),app.getTrainState(),app.getApplicationState(),app.getApplicationInfoId());
	}
	
	/**
	 * 删除内容
	 * 学生表 t_student
	 * @param value
	 * @throws Exception
	 */
	@Override
	public int delete(ApplicationInfo value) throws Exception {
		String sql="delete from t_application_info where application_info_id=?";
		return JdbcUtil.update(sql,value.getApplicationInfoId());
	}
	/**
	 * 查找所有的内容
	 * @param pageNo 第几页
	 * @param pageSize 每页数字
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<ApplicationInfo> findAll(int pageNo, int pageSize) throws Exception {
		String sql = "select * from t_application_info,t_student where t_application_info.stu_id=t_student.stu_id limit ?,?";
		return JdbcUtil.query(ApplicationInfo.class,sql,(pageNo-1)*pageSize,pageSize);
	}
	/**
	 * 查找单一实体
	 * @param Key
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<ApplicationInfo> findAll(Object key,int pageNo, int pageSize) throws Exception {
		String sql = "select * from t_application_info "
				+ "left join t_student  on t_application_info.stu_id=t_student.stu_id "
				+ "where stu_name=? limit ?,?";
		return JdbcUtil.query(ApplicationInfo.class,sql,key,(pageNo-1)*pageSize,pageSize);
	}
	
	/**
	 * String 对应  数据库 列名（comment)
	 * Object 对应相应列名值
	 * 返回的是所查找的类中的数据。方便EXCEL导出
	 */
	@Override
	public List<Map<String,Object>> getMapList() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public ApplicationInfo findByKey(Object key) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * 虞艳
	 * 提交了党员申请
	 * @param key 班级名称
	 * @param pageNo 页数
	 * @param pageSize 每页显示行数
	 * @return 查询到的学生
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<ApplicationInfo> findAllApplication(Object key,int pageNo, int pageSize) throws Exception {
		String sql="SELECT t_application_info.*,t_student.*  FROM  t_student";
		sql+=" INNER JOIN t_application_info ON t_student.stu_id=t_application_info.stu_id WHERE stu_class_name=? LIMIT ?,? ;";	
		List<ApplicationInfo> list=JdbcUtil.query(ApplicationInfo.class,sql,key,(pageNo-1)*pageSize,pageSize);
		return list;
	}
	
	
	/**
	 * 计算提交了党员申请的学生数量
	 * @param key 班级名称
	 * @param pageSize 每页显示行数
	 * @return 计算出的页数
	 * @throws Exception
	 */
	public long getCount(Object key ,int pageSize) throws Exception{
		String sql="SELECT  COUNT(*)  FROM  t_student";
		sql+=" INNER JOIN t_application_info ON t_student.stu_id=t_application_info.stu_id WHERE stu_class_name=?;";	
		long count = (long) JdbcUtil.uniqueValue(sql, 1, key);
		return count%pageSize==0?count/pageSize:count/pageSize+1;
	}
	
	/**
	 * 同意/拒绝入党申请
	 * @param appId
	 * @return
	 * @throws SQLException
	 */
	public int updateState(int appId,int state) throws SQLException{
		String sql="update t_application_info set application_state=? where application_info_id=?";
		return JdbcUtil.update(sql,state,appId);
	}
	
	/**
	 * 修改学生党员培训已完成
	 * @param stuId
	 * @return
	 * @throws SQLException
	 */
	public int stateCompleted(int stuId) throws SQLException{
		String sql="update t_application_info set train_state=2 where stu_id=?";
		return JdbcUtil.update(sql,stuId);
	}
	
	public long findAllCount() throws Exception{
		String sql="select count(*) from t_application_info";

		long page=(long)JdbcUtil.uniqueValue(sql, 1);

		return page%10==0?page/10:page/10+1;
	}
	
	
	public long findIdCount(Object key) throws Exception{
		String sql="select count(*) from t_application_info "
				+ "left join t_student on  t_application_info.stu_id=t_student.stu_id "
				+ "where t_application_info.application_info_id=? or t_application_info.stu_id=?";
		long page=(long)JdbcUtil.uniqueValue(sql, 1,key,key);
		return page%10==0?page/10:page/10+1;
	}
	
	public ApplicationInfo findByName(Object key) throws Exception{
		String sql= "select * from t_application_info "
				+ "left join t_student on t_application_info.stu_id=t_student.stu_id "
				+ "where t_student.stu_name=?";
		return (ApplicationInfo)JdbcUtil.uniqueBean(ApplicationInfo.class, sql, key);
	}

}
